package sskj.regulatory.presentation.screen.registeComplete

import android.content.Intent
import com.qmuiteam.qmui.widget.dialog.QMUIDialog
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog
import kotlinx.android.synthetic.main.fragment_register_complete.view.*
import sskj.regulatory.R
import sskj.regulatory.domain.entity.user.UserEntity
import sskj.regulatory.presentation.base_mvp.base.BaseFragment
import sskj.regulatory.presentation.screen.login.LoginFragment
import sskj.regulatory.presentation.screen.main.MainActivity
import sskj.regulatory.presentation.screen.register.RegisterCompleteContract
import sskj.regulatory.presentation.screen.register.RegisterCompletePresenter
import sskj.regulatory.presentation.utils.delegates.bundle
import javax.inject.Inject

class RegisteCompleteFragment : BaseFragment<RegisterCompleteContract.View, RegisterCompleteContract.Presenter>(), RegisterCompleteContract.View {

    private val userEntity: UserEntity by bundle()

    @Inject
    lateinit var mPresenter: RegisterCompletePresenter

    override fun injectDependencies() = activityComponent.inject(this)

    override fun initPresenter() = mPresenter

    override val layoutResId: Int = R.layout.fragment_register_complete

    override fun onBackPressed(): Boolean {
        baseFragmentActivity.popBackStack(LoginFragment::class.java)
        return false
    }

    private fun initEvent() {
        mCacheView.regsiterCompleteButton.setOnClickListener { mPresenter.login(userEntity.username, userEntity.password) }
    }

    override fun initView() {
        initEvent()
    }

    override fun loginOk() {
        startActivity(Intent(context, MainActivity::class.java))
        getActivity()?.finish()
    }

    override fun showError(message: String?) = QMUIDialog.MessageDialogBuilder(getActivity())
            .setTitle("错误提示")
            .setMessage("登录失败,失败原因:$message")
            .addAction("确定") { dialog, _ -> dialog.dismiss() }
            .create(R.style.QMUI_Dialog).show()

    override fun showLoading() {
        activity.tipDialog = QMUITipDialog.Builder(context)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                .setTipWord("加载中")
                .create()
        activity.tipDialog?.show()
    }


    override fun hideLoading() {
        activity.tipDialog?.dismiss()
    }
}